package com.totem.admin.service.impl;

import cn.hutool.core.map.MapUtil;
import com.totem.admin.service.AdminService;
import com.totem.admin.vo.FileUrlVO;
import com.totem.admin.vo.FirstPageVO;
import com.totem.base.constants.CommonConstants;
import com.totem.base.constants.ErrorCode;
import com.totem.base.enums.FileTypeEnum;
import com.totem.base.exception.BaseException;
import com.totem.base.util.S3Util;
import com.totem.customer.mapper.TCustomerMapper;
import com.totem.customer.mapper.TMemberMapper;
import com.totem.order.mapper.TWalletJournalMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private TCustomerMapper tCustomerMapper;
    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TWalletJournalMapper tWalletJournalMapper;

    @Override
    public FirstPageVO firstPage() {
        FirstPageVO vo = new FirstPageVO();
        ZoneId zid = ZoneId.systemDefault();
        LocalDateTime beginLdt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endLdt = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Date start = Date.from(beginLdt.atZone(zid).toInstant());
        Date end = Date.from(endLdt.atZone(zid).toInstant());

        Map<String, BigDecimal> sumRecharge= tWalletJournalMapper.sumRecharge();
        if(MapUtil.isNotEmpty(sumRecharge)){
            BigDecimal amount = sumRecharge.get("amount");
            BigDecimal charges = sumRecharge.get("charges");
            vo.setRechargeAmt(amount);
        }else{
            vo.setRechargeAmt(BigDecimal.ZERO);
        }
        Map<String, BigDecimal> sumTodayRecharge= tWalletJournalMapper.sumTodayRecharge(start, end);
        if(MapUtil.isNotEmpty(sumTodayRecharge)){
            BigDecimal amount = sumTodayRecharge.get("amount");
            BigDecimal charges = sumTodayRecharge.get("charges");
            vo.setTodayRechargeAmt(amount);
        }else{
            vo.setTodayRechargeAmt(BigDecimal.ZERO);
        }
        Map<String, BigDecimal> sumWithdraw= tWalletJournalMapper.sumWithdraw();
        if(MapUtil.isNotEmpty(sumWithdraw)){
            BigDecimal amount = sumWithdraw.get("amount");
            BigDecimal charges = sumWithdraw.get("charges");
            vo.setWithdrawAmt(amount);
        }else{
            vo.setWithdrawAmt(BigDecimal.ZERO);
        }
        Map<String, BigDecimal> sumTodayWithdraw= tWalletJournalMapper.sumTodayWithdraw(start, end);
        if(MapUtil.isNotEmpty(sumTodayWithdraw)){
            BigDecimal amount = sumTodayWithdraw.get("amount");
            BigDecimal charges = sumTodayWithdraw.get("charges");
            vo.setTodayWithDrawAmt(amount);
        }else{
            vo.setTodayWithDrawAmt(BigDecimal.ZERO);
        }
        vo.setWithdrawAble(vo.getRechargeAmt().subtract(vo.getWithdrawAmt()));
        return vo;
    }
}
